package de.hdu.myjobapp.view.view;

import java.util.Date;
import java.util.Vector;

import org.apache.catalina.startup.SetContextPropertiesRule;
import org.junit.Ignore;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Reindeer;

import de.hdu.myjobapp.ctrl.ApplicationController;
import de.hdu.myjobapp.model.Application;
import de.hdu.myjobapp.model.Rating;
import de.hdu.myjobapp.model.Status;
import de.hdu.myjobapp.view.MyJobApplication;
/**
 * Simple Editor Form to edit basic States for applications
 * @author MTA
 *
 */
public class StatusForm extends Window implements ClickListener{
	private Status status;
	private Application application;
	private BeanItem<Status> statusBean; 
	final Form form = new Form();
	private Button delete = new Button("Löschen");
	private Button save = new Button("Speichern");
	private Boolean neworold = false; //false ist old bzw. bestehend
	private ApplicationController appControl = (ApplicationController)MyJobApplication.getProject().getApplicationContext().getBean("applicationController");
	/**
	 * Overloaded Constructor to be able to edit existing Ratings
	 */
	public StatusForm(Application app, Status status){
		super("Statusfenster");
		this.status = status;
		this.application = app;
		this.neworold = false;
		buildview();
		
	}
	/**
	 * Standard Constructor to build a view to create a new Rating
	 * relating to an application
	 */
	public StatusForm(Application app){
		super("Statusfenster");
		this.status = new Status();
		this.application = app;
		this.neworold = true;
		buildview();
	}
	
	/**
	 * Create the Layout of the new Window
	 */
	@SuppressWarnings("unchecked")
	private void buildview(){
		Layout main = new VerticalLayout();
		setContent(main);
		main.setSizeUndefined();
		main.setStyleName(Reindeer.PANEL_LIGHT);
		
		statusBean = new BeanItem<Status>(this.status);
		
		 // Set form caption and description texts 
		form.setCaption("Rating");
		form.setDescription("Bitte geben Sie eine Beschreibung und eine Bewertung ihres Ratings ein. Die Bewertung ist numerisch. Mehr Punkte zählen mehr.");
	//	form.getField("Description").setWidth("200px");
		//	form.setHeight("250px");
		 // Bind the bean item as the data source for the form. 
		form.setItemDataSource(statusBean); 
		
		@SuppressWarnings("rawtypes")
		Vector reihenfolge = new Vector();

		reihenfolge.add("statusDatenew");
		reihenfolge.add("statusDescription");
		form.setVisibleItemProperties(reihenfolge);
		
		//Make the two fields required
		form.getField("statusDatenew").setRequired(true);
		form.getField("statusDatenew").setRequiredError("Beschreibung muss ausgefüllt werden");
	
		form.getField("statusDescription").setRequired(true);
		form.getField("statusDescription").setRequiredError("Es muss eine Bewertung abgegeben werden");

		// Enable buffering.
		form.setWriteThrough(true);
		
		//Enable Validation with Commit()
		form.setValidationVisible(true);
		form.getLayout().setHeight("200px");
		form.getLayout().setWidth("300px");
		
		main.addComponent(form);
		main.setWidth("300px");
		Layout buttons = new HorizontalLayout();
		delete.addListener(this);
		//neue Ratings können nicht gelöscht werden
		delete.setEnabled(false);
		//delete.setVisible(false);
		buttons.addComponent(delete);
		save.addListener(this);
		buttons.addComponent(save);
		main.addComponent(buttons);
	}
	@Override
	public void buttonClick(ClickEvent event) {
		if (event.getButton() == delete)
		{
			if(!neworold){
				/*
				 * It has been decided that Status Entries cannot be deleted
				 */
//				appControl.deleteRating(rating);
//				//Gibt eine Nachricht am Bildschirm aus
//				MyJobApplication.getProject().getMainWindow().showNotification("Rating gelöscht");
//				//aktualisiert die Tabelle
//				MyJobApplication.getProject().getUiHandler().setApplicationEditorApp(application, false);
			}
		}
		else if (event.getButton() == save)
		{
			if(neworold){
				form.commit();
				status.setApplicationId(application);
				status.setStatusDate(new Date());
				// Type 0 Objects are Status objects
				status.setStatusType(0);
				appControl.saveStatus(status);
				MyJobApplication.getProject().getUiHandler().setApplicationEditorApp(application, false);
			}else{
				form.commit();
				appControl.saveStatus(status);
				MyJobApplication.getProject().getUiHandler().setApplicationEditorApp(application, false);
			}
			
		}
		if (getParent() != null)
		{
			((Window) getParent()).removeWindow(this);
		}	
	}
	
	
	
	
}
